//
//  DYScreenShotVC.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYScreenShotVC.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <Masonry.h>
#import "UIColor+Additions.h"
#import "VPhotoAlbumCell.h"
#import "VPHFetchUtil.h"
#import "DYScreenShotModel.h"
#import "GSAConstFile.h"
#import <SVProgressHUD.h>
#import "FPPhotosTool.h"
#import "DYSimilarPicModel.h"
static NSString *photoAlbumCellID = @"photoAlbumCell";
CGFloat col = 4;
@interface DYScreenShotVC ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    CGFloat _margin;
}
@property (nonatomic, weak) UIButton *titleBtn;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, weak) UIButton *rightBarItem;
@property (nonatomic, strong) NSMutableArray *assetArray;
@end

@implementation DYScreenShotVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self configSubViews];
    [self configDataSource];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)dealloc{

}

#pragma mark -- private method

- (void)configSubViews
{
    _margin = 2;

    
//    self.gk_navTitleView = [self configTitleView];
//    self.gk_navRightBarButtonItem = [self rightBarButtonItem];
    [self.view addSubview:self.collectionView];
   
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navigationBar.bottom);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

- (void)configDataSource
{
    //
    
   
    
    
    //
    [self.collectionView reloadData];
}

- (UIView *)configTitleView
{
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 200, 44)];
    [button setTitleColor:[UIColor themeTitleColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:17.f];
    NSString *title = [NSString stringWithFormat:@"选定%lu张照片",(unsigned long)self.viewModel.seletedPicArray.count];
    [button setTitle:title forState:UIControlStateNormal];
    self.titleBtn = button;
    return button;
}

- (UIBarButtonItem *)rightBarButtonItem
{
    
    NSString *title = @"取消全选";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [button setTitleColor:[UIColor colorWithHexString:@"#5C92FF"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(comfireItemDidClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.rightBarItem = button;
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}


- (void)handleCellForSelectedButtonNumberShowEvent:(NSIndexPath *)currentIndexPath
{
    NSInteger index = currentIndexPath.item;
    //PHAsset *asset
    DYSimilarPicModel *asset = self.viewModel.originPicArray[index];
    if ([self.viewModel.seletedPicArray containsObject:asset]) {
        [self.viewModel.seletedPicArray removeObject:asset];
    }else{
        [self.viewModel.seletedPicArray addObject:asset];
    }
    NSString *btnTitle = self.viewModel.seletedPicArray.count == self.viewModel.originPicArray.count ? @"取消全选":@"选择全部";
    [self.rightBarItem setTitle:btnTitle forState:UIControlStateNormal];
    NSString *title = [NSString stringWithFormat:@"选定%lu张照片",(unsigned long)self.viewModel.seletedPicArray.count];
    [self.titleBtn setTitle:title forState:UIControlStateNormal];
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [FPPhotosTool browserImagesWithAssetArray:self.assetArray index:indexPath.item];

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.view.bounds.size.width - _margin * (col + 1)) / col, (self.view.bounds.size.width - _margin * (col + 1)) / col);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.originPicArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
      VPhotoAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:photoAlbumCellID forIndexPath:indexPath];
    DYSimilarPicModel *asset = self.viewModel.originPicArray[indexPath.item];

    [cell updateCellContentWithSelectedModel:asset selectedAssetArray:self.viewModel.seletedPicArray];
        __weak typeof(self) weakSelf = self;
        cell.selectPhotoCompleteBlock = ^{
            [weakSelf handleCellForSelectedButtonNumberShowEvent:indexPath];
        };
      return cell;
    
}

#pragma mark --------action--------

- (void)leftBarButtonItemDidClick{
    !self.seletedPhotoCompleteBlock ?:self.seletedPhotoCompleteBlock(self.viewModel);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)comfireItemDidClick
{
    @weakify(self)
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        @strongify(self)
        if (self.viewModel.seletedPicArray.count > 0) {
            [self.viewModel.seletedPicArray removeAllObjects];
        }else{
            [self.viewModel.seletedPicArray addObjectsFromArray:self.viewModel.originPicArray];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *btnTitle = self.viewModel.seletedPicArray.count > 0 ? @"取消全选":@"选择全部";
            [self.rightBarItem setTitle:btnTitle forState:UIControlStateNormal];
            NSString *title = @"";
            if (self.viewModel.seletedPicArray.count > 0) {
                title = [NSString stringWithFormat:@"选定%lu张照片",(unsigned long)self.viewModel.seletedPicArray.count];
            }else{
                title = @"屏幕截图";
            }
            [self.titleBtn setTitle:title forState:UIControlStateNormal];
            [self.collectionView reloadData];
        });
    });
}

#pragma mark - getter

- (void)setViewModel:(DYSimilarPhotoVCModel *)viewModel{
    _viewModel = viewModel;
    [self.viewModel.seletedPicArray removeAllObjects];
    [self.viewModel.seletedPicArray addObjectsFromArray:self.viewModel.originPicArray];
    for (DYSimilarPicModel *model in self.viewModel.originPicArray) {
        PHAsset *asset = model.asset;
        [self.assetArray addObject:asset];
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.allowsMultipleSelection = YES;
        _collectionView.bounces = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[VPhotoAlbumCell class] forCellWithReuseIdentifier:photoAlbumCellID];
       
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(100, 100);
        [_flowLayout setSectionInset:UIEdgeInsetsMake(0, _margin, 0, _margin)];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout.minimumLineSpacing = _margin;
        _flowLayout.minimumInteritemSpacing = _margin;
    }
    return _flowLayout;
}

- (NSMutableArray *)assetArray{
    if (!_assetArray) {
        _assetArray = [NSMutableArray new];
    }
    return _assetArray;
}

@end

